using UnityEngine;
using UnityEngine.UI;

[ExecuteInEditMode]
public class AnchorTextText : MonoBehaviour
{
	private RectTransform rectTransform;

	public Text text1;

	public Text text2;

	public HAlign horizontalAlign = HAlign.center;

	public float spaceBetween = 2f;

	private Vector2 currentText1Size;

	private Vector2 currentText2Size;

	private Vector2 currentAnchorSize;

	private void Start()
	{
		currentText1Size = Vector2.zero;
		currentText2Size = Vector2.zero;
		currentAnchorSize = Vector2.zero;
		GenericDialog.DialogEnabledEvent += OnDialogEnabled;
	}

	private void Destoy()
	{
		GenericDialog.DialogEnabledEvent -= OnDialogEnabled;
	}

	private void OnDialogEnabled()
	{
		DoAlign();
	}

	private void Update()
	{
		if (currentText2Size != text2.rectTransform.sizeDelta || currentText1Size != text1.rectTransform.sizeDelta)
		{
			DoAlign();
		}
	}

	private void DoAlign()
	{
		if (rectTransform == null)
		{
			rectTransform = GetComponent<RectTransform>();
		}
		currentText1Size = text1.rectTransform.sizeDelta;
		currentText2Size = text2.rectTransform.sizeDelta;
		currentAnchorSize = rectTransform.sizeDelta;
		Vector2 anchoredPosition = text1.rectTransform.anchoredPosition;
		Vector2 anchoredPosition2 = text2.rectTransform.anchoredPosition;
		float num = currentText1Size.x + spaceBetween + currentText2Size.x;
		if (horizontalAlign == HAlign.center)
		{
			anchoredPosition.x = rectTransform.anchoredPosition.x - num / 2f + currentText1Size.x / 2f;
			anchoredPosition2.x = rectTransform.anchoredPosition.x + num / 2f - currentText2Size.x / 2f;
			anchoredPosition.y = rectTransform.anchoredPosition.y;
			anchoredPosition2.y = rectTransform.anchoredPosition.y;
		}
		else if (horizontalAlign == HAlign.left)
		{
			anchoredPosition.x = rectTransform.anchoredPosition.x - currentAnchorSize.x / 2f + currentText1Size.x / 2f;
			anchoredPosition2.x = rectTransform.anchoredPosition.x - currentAnchorSize.x / 2f + currentText1Size.x + spaceBetween + currentText2Size.x / 2f;
			anchoredPosition.y = rectTransform.anchoredPosition.y;
			anchoredPosition2.y = rectTransform.anchoredPosition.y;
		}
		else if (horizontalAlign == HAlign.right)
		{
			anchoredPosition.x = rectTransform.anchoredPosition.x + currentAnchorSize.x / 2f - currentText2Size.x - spaceBetween - currentText1Size.x / 2f;
			anchoredPosition2.x = rectTransform.anchoredPosition.x + currentAnchorSize.x / 2f - currentText2Size.x / 2f;
			anchoredPosition.y = rectTransform.anchoredPosition.y;
			anchoredPosition2.y = rectTransform.anchoredPosition.y;
		}
		text1.rectTransform.anchoredPosition = anchoredPosition;
		text2.rectTransform.anchoredPosition = anchoredPosition2;
	}
}
